import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    redirect: "/home"
  },
  {
    path: '/home',
    name: 'home',
    meta:{
      require:true
    },
    component: () => import('../views/Home.vue'),
    children: [
      {
        path: "/user_add",
        name: "add",
        component: () => import('../views/user/User_Add.vue'),
      },
      {
        path: '/user_del',
        name: "del",
        component: () => import('../views/user/User_del.vue'),
      },
      {
        path: '/user_query',
        name: "query",
        component: () => import('../views/user/User_query.vue'),
      }
    ]
  },
  {
    path: "/test",
    name: "test",
    component: () => import("../views/Test.vue")
  },
   {
    path: "/login",
    name: "login",
    component: () => import("../views/Login.vue")
  }

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to,from,next)=>{
  if (to.meta.require&&!window.localStorage.getItem('token')) {
     next("/login")
  }
  else{
    next()
  }
})
export default router
